package zzs.generalframework.console.core.parameter.commonparameter.department.dao.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import zzs.generalframework.common.core.parameter.commonparameter.department.DepartmentInfo;
import zzs.generalframework.common.core.parameter.commonparameter.department.DepartmentPO;
import zzs.generalframework.console.core.parameter.commonparameter.department.dao.IDepartmentDao;
import zzs.generalframework.console.util.QueryParameter;
import zzs.generalframework.util.core.string.StringUtils;

/**
 * @title
 * @description
 * @project zzs-console
 * @package zzs.generalframework.console.core.parameter.commonparameter.department.dao.impl
 * @author zzs
 * @date 2018年2月2日
 * @version 1.0
 */
@Repository("zzs.generalframework.console.core.parameter.commonparameter.department.dao.impl.departmentDaoImpl")
public class DepartmentDaoImpl implements IDepartmentDao
{
	private Logger logger = LoggerFactory.getLogger(DepartmentDaoImpl.class);
	@PersistenceContext(unitName = "zzs-entity")
	EntityManager entityManager;
	@Autowired
	JdbcTemplate jdbcTemplate;

	@Override
	public boolean insert(DepartmentPO departmentPO)
	{
		logger.info("====插入部门数据开始====");
		logger.debug("====插入部门数据,入参:DepartmentPO=[{}]====", departmentPO);
		entityManager.persist(departmentPO);
		logger.debug("====插入部门数据,出参:boolean=[{}]====", true);
		logger.info("====插入部门数据结束====");
		return true;
	}

	@Override
	public boolean update(DepartmentPO departmentPO)
	{
		logger.info("====更新部门数据开始====");
		logger.debug("====更新部门数据,入参:DepartmentPO=[{}]====", departmentPO);
		entityManager.merge(departmentPO);
		logger.debug("====更新数据,出参:boolean=[{}]====", true);
		logger.info("====更新部门数据结束====");
		return true;
	}

	@Override
	public List<DepartmentPO> select(DepartmentInfo departmentInfo)
	{
		logger.info("====查询部门数据开始====");
		logger.debug("====查询部门数据,入参:DepartmentInfo=[{}]====", departmentInfo);
		List<DepartmentPO> returnList = null;
		StringBuffer sqlBuffer = new StringBuffer();
		QueryParameter parameter = new QueryParameter();
		sqlBuffer.append("select a from DepartmentPO a where 1=1 ");
		if (StringUtils.isNotNullAndBlank(departmentInfo.getDepartmentID()))
		{
			sqlBuffer.append(" and a.departmentID=?");
			parameter.putParameterObject(departmentInfo.getDepartmentID());
		}
		if (StringUtils.isNotNullAndBlank(departmentInfo.getDepartmentCode()))
		{
			sqlBuffer.append(" and a.departmentCode=?");
			parameter.putParameterObject(departmentInfo.getDepartmentCode());
		}
		if (StringUtils.isNotNullAndBlank(departmentInfo.getDepartmentName()))
		{
			sqlBuffer.append(" and a.departmentName=?");
			parameter.putParameterObject(departmentInfo.getDepartmentName());
		}
		if (StringUtils.isNotNullAndBlank(departmentInfo.getDepartmentDesc()))
		{
			sqlBuffer.append(" and a.departmentDesc=?");
			parameter.putParameterObject(departmentInfo.getDepartmentDesc());
		}
		if (StringUtils.isNotNullAndBlank(departmentInfo.getOrderNumber()))
		{
			sqlBuffer.append(" and a.orderNumber=?");
			parameter.putParameterObject(departmentInfo.getOrderNumber());
		}
		if (StringUtils.isNotNullAndBlank(departmentInfo.getRecordStatus()))
		{
			sqlBuffer.append(" and a.recordStatus=?");
			parameter.putParameterObject(departmentInfo.getRecordStatus());
		}
		logger.info("查询数据SQL:[{}]", sqlBuffer.toString());
		Query query = entityManager.createQuery(sqlBuffer.toString());
		parameter.setQueryParameter(query);
		returnList = query.getResultList();
		logger.debug("====查询部门数据,出参:List<DepartmentPO>=[{}]====", returnList);
		logger.info("====查询部门数据结束====");
		return returnList;
	}

	@Override
	public boolean delete(DepartmentPO departmentPO)
	{
		logger.info("====删除部门数据开始====");
		logger.debug("====删除部门数据,入参:DepartmentPO=[{}]====", departmentPO);
		entityManager.remove(departmentPO);
		logger.debug("====删除部门数据,出参:boolean=[{}]====", true);
		logger.info("====删除部门数据结束====");
		return true;
	}

	@Override
	public Long count(DepartmentPO departmentPO)
	{
		logger.info("====统计部门数据开始====");
		logger.debug("====统计部门数据,入参:DepartmentPO=[{}]====", departmentPO);
		Long returnLong = null;
		StringBuffer sqlBuffer = new StringBuffer();
		QueryParameter parameter = new QueryParameter();
		sqlBuffer.append("select count(*) from DepartmentPO a where 1=1 ");
		if (StringUtils.isNotNullAndBlank(departmentPO.getDepartmentID()))
		{
			sqlBuffer.append(" and a.departmentID=?");
			parameter.putParameterObject(departmentPO.getDepartmentID());
		}
		if (StringUtils.isNotNullAndBlank(departmentPO.getDepartmentCode()))
		{
			sqlBuffer.append(" and a.departmentCode=?");
			parameter.putParameterObject(departmentPO.getDepartmentCode());
		}
		if (StringUtils.isNotNullAndBlank(departmentPO.getDepartmentName()))
		{
			sqlBuffer.append(" and a.departmentName=?");
			parameter.putParameterObject(departmentPO.getDepartmentName());
		}
		if (StringUtils.isNotNullAndBlank(departmentPO.getDepartmentDesc()))
		{
			sqlBuffer.append(" and a.departmentDesc=?");
			parameter.putParameterObject(departmentPO.getDepartmentDesc());
		}
		if (StringUtils.isNotNullAndBlank(departmentPO.getOrderNumber()))
		{
			sqlBuffer.append(" and a.orderNumber=?");
			parameter.putParameterObject(departmentPO.getOrderNumber());
		}
		if (StringUtils.isNotNullAndBlank(departmentPO.getRecordStatus()))
		{
			sqlBuffer.append(" and a.recordStatus=?");
			parameter.putParameterObject(departmentPO.getRecordStatus());
		}
		logger.info("统计数据SQL:[{}]", sqlBuffer.toString());
		Query query = entityManager.createQuery(sqlBuffer.toString());
		parameter.setQueryParameter(query);
		returnLong = (Long) query.getSingleResult();
		logger.debug("====统计部门数据,出参:Long=[{}]====", returnLong);
		logger.info("====统计部门数据结束====");
		return returnLong;
	}

}
